#!/bin/sh
#
# Description:
#	Script will receive 3 Arguments (Project Name, Environment, Branch)
#	Various things happening here:
#	1.) Changing to the repo directory
#	2.) Resetting the local master branch to look exactly like remote origin master branch
# 	3.) Clean off any files that don't need to be there
#	4.) Pull just to be doubly sure we have all the latest changes
#	5.) Switches branch to master just to be extra sure that we are on the right
#		branch and our working tree has the right files.
#
# Written by: LM
#
# DIR URL BRANCH
if [ -n "$1" ] ; then
	DIR=$1
else
	DIR="/opt/work/sites/www"
fi

if [ -n "$2" ] ; then
	URL=$2
else
	URL="http://www"
fi

if [ -n "$3" ] ; then
	BRANCH=$3
else
	BRANCH="master"
fi
echo "DIR" $DIR
echo "URL" $URL
echo "BRANCH" $BRANCH
if [ ! -d $DIR ] ; then
    echo "repository not exist, try to clone"
    git clone $URL $DIR
else
    echo "repository already exist, try to update"
    cd $DIR
    git reset --hard origin/$BRANCH
    git clean -f
    git pull
    git checkout $BRANCH
fi

# forever stop index.js
# NODE_ENV=production forever start index.js
echo "#-----------------------------------------------#"
echo "#              Execution Completed              #"
echo "#-----------------------------------------------#"